package com.zhao.dota.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhao.dota.model.Quotation;
import com.zhao.dota.service.QuotationService;
import com.zhao.dota.struct.Result;
import com.zhao.dota.vo.QuotationVo;
import com.zhao.dota.vo.RollVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/{leagueId}/quotation")
public class QuotationController {

    @Resource
    private QuotationService quotationService;

    @GetMapping("/")
    public Result<List<QuotationVo>> quotation(@PathVariable Integer leagueId, Integer roundId) {
        List<QuotationVo> list = quotationService.findByRoundId(roundId);
        list.forEach(QuotationVo::setHeroAvatar);
        return Result.success(list);
    }

    @PostMapping("/")
    public Result<Quotation> save(@PathVariable Integer leagueId, Quotation quotation) {
        Quotation save = quotationService.save(leagueId, quotation);
        return Result.success(save);
    }

    @PostMapping("/otherQuotation")
    public Result<Quotation> otherQuotation(@PathVariable Integer leagueId, Quotation quotation) {
        Quotation save = quotationService.otherQuotation(leagueId, quotation);
        return Result.success(save);
    }

    @DeleteMapping("/{id}")
    public Result<Void> cancel(@PathVariable Integer id) {
        boolean b = quotationService.cancel(id);
        return Result.complete(b);
    }

    @GetMapping("/notifyRoll")
    public Result<Void> notifyRoll(@PathVariable Integer leagueId, Integer roundId) {
        quotationService.sendRollWebSocket(roundId);
        return Result.success();
    }

    @PostMapping("/roll")
    public Result<Integer> roll(@PathVariable Integer leagueId, Integer roundId, Integer heroId) {
        int point = quotationService.roll(roundId, heroId);
        return Result.success(point);
    }

    @GetMapping("/needRolls")
    public Result<List<RollVo>> needRolls(@PathVariable Integer leagueId) {
        List<RollVo> list = quotationService.needRolls(leagueId);
        return Result.success(list);
    }

}
